package com.ptit.giaothong;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

import com.ptit.giaothong.bienbao.Itembb;
import com.ptit.giaothong.database.DatabaseHelper;

public class DelayActivity extends Activity {
	public static long total_time;
	public static CountDownTimer countDownTimer;
	public static long timer;
	public static DatabaseHelper myDbHelper = null;
	public static List<Itembb> listBienbao = null;

	private final static int REFRESH_RATE = 1000;
	private int myProgress = 0;
	private ProgressBar pb;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.loadding);
		// delayRom(5);
		pb = (ProgressBar) findViewById(R.id.prbVtcLoading);
		pb.setProgressDrawable(getResources()
				.getDrawable(R.drawable.vtcloading));
		new Thread(myThread).start();

		myDbHelper = new DatabaseHelper(DelayActivity.this);
		try {
			myDbHelper.createDataBase();
			myDbHelper.openDataBase();

		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		} catch (SQLException sqle) {
			throw sqle;
		}
	}

	public void delayRom(int time) {
		final Date date = new Date();
		total_time = time * 1000;
		countDownTimer = new CountDownTimer(total_time, REFRESH_RATE) {
			public void onTick(long millisUntilFinished) {
				total_time = (millisUntilFinished / 1000);

				int houre = (int) (total_time / 3600);
				int sdHoure = (int) (total_time % 3600);
				int minute = sdHoure / 60;
				int sec = sdHoure % 60;
				date.setHours((int) (houre));
				date.setMinutes(minute);
				date.setSeconds(sec);
				timer = total_time;
			}

			public void onFinish() {
				finish();
				Intent intent = new Intent(DelayActivity.this,
						MainActivity.class);
				startActivity(intent);
			}
		}.start();
	}

	private Runnable myThread = new Runnable() {
		@Override
		public void run() {
			while (myProgress < 100) {
				try {
					pb.setProgress(myProgress);
					myHandle.sendMessage(myHandle.obtainMessage());
					Thread.sleep(25);
				} catch (Throwable t) {
				}
			}
		}

		Handler myHandle = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				myProgress += 1;
				if (myProgress == 100) {
					finish();
					Intent intent = new Intent(DelayActivity.this,
							MainActivity.class);
					startActivity(intent);

				}
			}
		};
	};

}
